#include "ui.h"

#include <iostream>

#include "config.h"
#include "screen_buffer.h"
#include "event.h"

MainWind::MainWind(const wxString& title) : MainWindGen(NULL, wxID_ANY, title) {
  SetInitialSize(wxSize(SCREEN_WIDTH + 124, SCREEN_HEIGHT + 50));
  m_screen->SetInitialSize(wxSize(SCREEN_WIDTH + 10, SCREEN_HEIGHT + 40));

  uint32_t divheight = SCREEN_HEIGHT / SCREEN_BUFFER_HEIGHT;
  uint32_t divwith = SCREEN_WIDTH / SCREEN_BUFFER_WIDTH;

  this->scale = divheight;
  if (divwith < divheight) {
    this->scale = divwith;
  }
  if (scale == 0) {
    scale = 1;
  }

  std::cout << "scale " << scale << " screen: with " << SCREEN_BUFFER_WIDTH
            << "  height " << SCREEN_BUFFER_HEIGHT << std::endl;
}

void MainWind::m_screenOnPaint(wxPaintEvent& event) {
  wxPaintDC dc(this->m_screen);

  wxImage img(SCREEN_BUFFER_WIDTH, SCREEN_BUFFER_HEIGHT,
              screen_buffer_get_data(), true);
  wxBitmap bmp(img.Scale(img.GetWidth() * scale, img.GetHeight() * scale));

  dc.DrawBitmap(bmp, 0, 0);
}

void MainWind::btncall(uint32_t btnID){
   event_do_click(btnID);
   this->m_screen->Refresh();
   this->m_screen->Update(); 
}

void MainWind::btnUPClicked(wxCommandEvent& event) {
  std::cout << "btn up clicked" << std::endl;
  btncall(EVENT_BTN_UP);
}

void MainWind::btnLEFTClicked(wxCommandEvent& event) {
  std::cout << "btn left clicked" << std::endl;
  btncall(EVENT_BTN_LEFT);
}
void MainWind::btnRIGHTClicked(wxCommandEvent& event) {
  std::cout << "btn right clicked" << std::endl;
  btncall(EVENT_BTN_RIGHT);
}
void MainWind::btnDOWNClicked(wxCommandEvent& event) {
  std::cout << "btn down clicked" << std::endl;
  btncall(EVENT_BTN_DOWN);
}
void MainWind::btnFUNC1Clicked(wxCommandEvent& event) {
  std::cout << "btn func1 clicked" << std::endl;
  btncall(EVENT_BTN_FUNC1);
}
void MainWind::btnFUNC3Clicked(wxCommandEvent& event) {
  std::cout << "btn func2 clicked" << std::endl;
  btncall(EVENT_BTN_FUNC2);
}
void MainWind::btnFUNC4Clicked(wxCommandEvent& event) {
  std::cout << "btn func3 clicked" << std::endl;
  btncall(EVENT_BTN_FUNC3);
}
void MainWind::btnFUNC5Clicked(wxCommandEvent& event) {
  std::cout << "btn func4 clicked" << std::endl;
  btncall(EVENT_BTN_FUNC4);
}
void MainWind::btnFUNC6Clicked(wxCommandEvent& event) {
  std::cout << "btn func5 clicked" << std::endl;
  btncall(EVENT_BTN_FUNC5);
}
